﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using AmiEmuLib.Properties;
using AmiEmuLib.Configuration.EventBindingConfig;
using AmiEmuLib.Host;
using log4net;
using TomanuExtensions;

namespace AmiEmuLib.Configuration
{
    /// <summary>
    /// Application configuration. 
    /// </summary>
    public class Config
    {
        private static string m_romsDirectory = Environment.GetFolderPath(
            Environment.SpecialFolder.DesktopDirectory);

        /// <summary>
        /// Logger.
        /// </summary>
        private static readonly ILog Logger = 
            LogManager.GetLogger(typeof(Config).GetParentFullName());

        /// <summary>
        /// Place where we keep our config.
        /// </summary>
        private static readonly string ConfigPath = HostEnvironment.SettingsDir + "Config.xml";

        /// <summary>
        /// Singleton instance.
        /// </summary>
        public static readonly Config Instance = new Config();

        /// <summary>
        /// Constructor.
        /// </summary>
        private Config()
        {
            if (!new FileInfo(ConfigPath).Exists)
                return;

            try
            {
                using (var fs = new FileStream(ConfigPath, FileMode.Open, FileAccess.Read))
                {
                    XmlReaderExtensions.ReadXml(fs, (reader) =>
                    {
                        reader.ReadStartElement(GetType().Name);

                        m_romsDirectory = reader.ReadElementString("RomsDirectory");

                        reader.ReadEndElement();
                    });
                }
            }
            catch (IOException)
            {
                Logger.ErrorFormat("Failed to load application xml configuration: '{0}'", ConfigPath);
            }

            HostEnvironment.HostWindowDetached += SaveToXml;
        }

        /// <summary>
        /// Saves config to xml.
        /// </summary>
        private void SaveToXml()
        {
            try
            {
                XmlWriterExtensions.WriteXml(ConfigPath, (writer) =>
                {
                    writer.WriteStartElement(GetType().Name);

                    writer.WriteElement("RomsDirectory", RomsDirectory);

                    writer.WriteEndElement();
                });
            }
            catch (IOException ex)
            {
                Logger.ErrorFormat("Failed to save application xml configuration: '{0}'", ConfigPath);
                throw ex;
            }
        }

        /// <summary>
        /// Directory where we search for roms. Also in subdirectories.
        /// </summary>
        public string RomsDirectory
        {
            get 
            {
                return m_romsDirectory;
            }
            set
            {
                m_romsDirectory = value;
                SaveToXml();
            }
        }
    }
}
